package com.app.qbake.module.publish.adapter;

import android.app.Activity;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.hannesdorfmann.adapterdelegates3.AdapterDelegate;

import com.wind.base.adapter.DisplayItem;
import com.wind.base.widget.WidthRatioFrameLayout;
import com.app.qbake.R;
import com.app.qbake.base.bean.UploadPhoto;
import com.app.qbake.module.publish.event.PhotoPreviewEvent;
import com.app.qbake.module.publish.event.PhotoRemoveEvent;
import com.wind.view.WdImageLoader;

import org.greenrobot.eventbus.EventBus;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by wind on 2017/3/8.
 */

public class PhotoPathItemDelegate extends AdapterDelegate<List<DisplayItem>> {
    private LayoutInflater mLayoutInflate;
    private Activity mActivity;
    private boolean hideDelete = false;
    private int mLayoutRes;
    public PhotoPathItemDelegate(Activity activity,boolean hideDelete,int layoutRes) {
        this(activity,hideDelete);
        mLayoutRes=layoutRes;
    }
    public PhotoPathItemDelegate(Activity activity,boolean hideDelete) {
        mLayoutInflate = LayoutInflater.from(activity);
        this.mActivity = activity;
        this.hideDelete = hideDelete;
    }

    @Override
    protected boolean isForViewType(@NonNull List<DisplayItem> items, int position) {
        UploadPhoto item= (UploadPhoto) items.get(position);
        return !TextUtils.isEmpty(item.getPath());
    }

    @NonNull
    @Override
    protected RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent) {
        int layout= R.layout.item_photo_with_del;
        if (mLayoutRes!=0){
            layout=mLayoutRes;
        }
        return new PhotoPathViewHolder(mLayoutInflate.inflate(layout, parent, false));
    }

    @Override
    protected void onBindViewHolder(@NonNull List<DisplayItem> items, final int position, @NonNull RecyclerView.ViewHolder holder, @NonNull List<Object> payloads) {
        PhotoPathViewHolder vh = (PhotoPathViewHolder) holder;


        UploadPhoto item= (UploadPhoto) items.get(position);
        WdImageLoader.display(mActivity, vh.iv, item.getPath());

        vh.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //照片预览
                EventBus.getDefault().post(new PhotoPreviewEvent(position));
            }
        });

        if (vh.iv_del!=null) {
            if (hideDelete) {
                vh.iv_del.setVisibility(View.GONE);
            } else {
                vh.iv_del.setVisibility(View.VISIBLE);
            }

            vh.iv_del.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    EventBus.getDefault().post(new PhotoRemoveEvent(position));
                }
            });
        }
    }

    static class PhotoPathViewHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.iv)
        ImageView iv;
        @Nullable
        @BindView(R.id.iv_del)
        ImageView iv_del;
        @Nullable
        @BindView(R.id.ratio_fl)
        WidthRatioFrameLayout ratio_fl;
        public PhotoPathViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
}
